﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Web;
using System.Xml;
using Castle.Services.Transaction;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic.Interface;

namespace Meqs.Logic.Implement
{
    public abstract class AbEvalSheetsLogic : CommonLogic<EvalSheets, int>, IEvalSheetsLogic
    {
        #region Implementation of ICommonLogic<EvalSheets, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(EvalSheets entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(EvalSheets entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int Delete(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("EvalSheets.Delete", ht);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("EvalSheets.Delete", ht);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public EvalSheets FindById(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByID<EvalSheets>("EvalSheets.FindById", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int FindCount(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByCount("EvalSheets.FindCount", ht);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<EvalSheets> FindAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<EvalSheets>("EvalSheets.Find", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<EvalSheets> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        /// <summary>
        /// 保存或更新EvalSheet
        /// </summary>
        /// <param name="sheets"></param>
        /// <param name="teachCode"></param>
        /// <param name="schoolMark"></param>
        /// <returns>
        /// -1: Exception
        /// </returns>
        public int SaveOrUpdateEvalSheet(EvalSheets sheets, string teachCode, string schoolMark)
        {
            if (sheets == null) return -1;
            Hashtable ht = new Hashtable();

            ht.Add("EvalSheets", sheets);
            ht.Add("TeachCode", teachCode);
            ht.Add("SchoolMark", schoolMark);
            if (sheets.RID == 0)
            {
                // 新增
                return (int)InsertCustomer("EvalSheets.SaveEvalSheet", ht);
            }
            // 更新
            UpdateCustomer("EvalSheets.UpdateEvalSheet", ht);
            return sheets.RID;
        }

        /// <summary>
        /// 删除EvalSheet
        /// </summary>
        /// <param name="sheetID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns> 
        [Transaction(TransactionMode.Requires)]
        public int DeleteEvalSheet(int sheetID, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", sheetID);
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("EvalSheets.DeleteEvalSheet", ht);
        }

        /// <summary>
        /// 根据seid删除EvalSheet
        /// </summary>
        [Transaction(TransactionMode.Requires)]
        public int DeleteEvalSheetByStudentEval(int SeID, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SEID", SeID);
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("EvalSheets.DeleteEvalSheetByStudentEval", ht);
        }

        /// <summary>
        /// 查询对象
        /// </summary>
        /// <param name="sheetID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public EvalSheetsExtend FindExtendById(int sheetID, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", sheetID);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByID<EvalSheetsExtend>("EvalSheets.FindExtendById", ht);
        }

        /// <summary>
        /// 查询当前对象
        /// </summary>
        /// <param name="teachID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public EvalSheetsExtend FindCurrentExtend(int teachID, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", teachID);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByID<EvalSheetsExtend>("EvalSheets.FindCurrentExtend", ht);
        }

        /// <summary>
        /// 获取日期列表
        /// </summary>
        /// <param name="teachID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<DateTime> GetDateList(int teachID, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("TeachID", teachID);
            ht.Add("SchoolMark", schoolMark);
            string taskIDList = FindCustomerByID<string>("EvalSettings.GetTaskIDListByTeacher", ht);

            ht.Clear();
            ht.Add("TaskIDList", taskIDList);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<DateTime>("EvalSheets.GetDateList", ht);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sheetsExtend"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int UpdateEvalSheet(EvalSheetsExtend sheetsExtend, string schoolMark)
        {
            if (sheetsExtend == null) return -1;
            Hashtable ht = new Hashtable();
            ht.Add("EvalSheets", sheetsExtend);
            ht.Add("SchoolMark", schoolMark);

            // 更新
            UpdateCustomer("EvalSheets.UpdateEvalSheet1", ht);
            return sheetsExtend.RID;
        }


        /// <summary>
        /// 查询学生评项目
        /// </summary>
        public ArrayList GetStudentEvals(int evalId, String schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("SEID", evalId);
            return FindCustomer("EvalSheets.GetStudentEvals", ht) as ArrayList;
        }

        /// <summary>
        /// 创建学生评价项
        /// </summary>
        public int CreateStudentEval(EvalSheets sheets, String schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("EvalSheets", sheets);
            return (int)InsertCustomer("EvalSheets.CreateStudentEval", ht);
        }

        /// <summary>
        /// 获取有评估的月份
        /// </summary>
        public ArrayList GetEvalMonth(Hashtable ht)
        {
            return FindCustomer("EvalSheets.GetEvalMonth", ht) as ArrayList;
        }

        /// <summary>
        /// 查询表单表里面的所有月份
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        public ArrayList GetEvalAllMonth(Hashtable ht)
        {
            return FindCustomer("EvalSheets.GetAllEvalMonth", ht) as ArrayList;

        }

        /// <summary>
        /// 判断检查单的修改状态
        /// true:  可修改
        /// false: 不可修改
        /// </summary>
        /// <param name="sheetID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public bool GetModifyState(int sheetID, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("EsID", sheetID);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByID<int>("SheetItems.FindCount", ht) == 0;
        }

        /// <summary>
        /// 获取当前EvalSheets
        /// </summary>
        /// <param name="teacherID"></param>
        /// <param name="seID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public EvalSheets GetEvalSheetsByTeacherStudentEval(int teacherID, int seID, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("TeacherID", teacherID);
            ht.Add("SEID", seID);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByID<EvalSheets>("EvalSheets.GetEvalSheetsByTeacherStudentEval", ht);
        }


        #region abstract

        /// <summary>
        /// 获取教师评估项树的xml文件内容
        /// </summary>
        /// <param name="teachID"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public abstract string GetSheetItemXmlStr(int teachID, string startDate, string endDate, string schoolMark);

        /// <summary>
        /// 获取教师评估项树的xml文件内容
        /// </summary>
        /// <param name="teachID"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public abstract object[] GetSheetItemXmlStr1(int teachID, string startDate, string endDate, string schoolMark);

        /// <summary>
        /// 获取时间范围内所有检查单所属的检查项
        /// </summary>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <param name="schoolMark"></param>
        /// <param name="groupType"></param>
        /// <param name="groupID"></param>
        /// <returns></returns>
        public abstract IList<EvalRateView> GetEvaledEvalItems(DateTime begin, DateTime end, String schoolMark, int groupType,
                                                      int groupID);

        /// <summary>
        /// 个人单项报表
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <param name="evalItemId"></param>
        /// <param name="teacherId"></param>
        /// <returns></returns>
        public abstract IList<PersonSingleReportView> GetPersonSingleReport(String schoolMark, DateTime begin, DateTime end,
                                                                   int evalItemId, int teacherId);

        /// <summary>
        /// 个人汇总报表
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <param name="teacherId"></param>
        /// <returns></returns>
        public abstract IList<PersonSummaryReportView> GetPersonSummaryReport(String schoolMark, DateTime begin, DateTime end,
                                                                     int teacherId);

        /// <summary>
        /// 单项汇总报表
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <param name="evalItemId"></param>
        /// <param name="groupType"></param>
        /// <param name="groupID"></param>
        /// <returns></returns>
        public abstract IList<SingleSummaryReportView> GetSingleSummaryReport(String schoolMark, DateTime begin, DateTime end,
                                                                     int evalItemId, int groupType, int groupID);

        /// <summary>
        /// 根据学校标识获取某个学校的教师评分汇总
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public abstract IList<ReportCollectView> GetReportCollectView(String schoolMark, DateTime begin, DateTime end);
        
        #endregion

    }
}
